package com.ymatou.iapi.godmch.testcase;

import java.util.HashMap;
import java.util.List;
import java.util.Map;




import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.common.collect.Lists;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.ymatou.iapi.godmch.parameter.editmchBean;
import com.ymatou.iapi.godmch.parameter.getAgentPromoteRelationBean;
import com.ymatou.iapi.godmch.parameter.registerByCodeBean;
import com.ymatou.iapi.godmch.service.applyAgentCall;
import com.ymatou.iapi.godmch.service.getAgentPromotePicCall;
import com.ymatou.iapi.godmch.service.getAgentPromoteRelationCall;
import com.ymatou.iapi.godmch.service.registerByCodeCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.agent_type;
import com.ymttest.database.model.invite_code;
import com.ymttest.database.model.mch_relation;
import com.ymttest.database.sqlwapper.godmch.agent_typeWapper;
import com.ymttest.database.sqlwapper.godmch.country_infoWapper;
import com.ymttest.database.sqlwapper.godmch.invite_codeWapper;
import com.ymttest.database.sqlwapper.godmch.mch_infoWapper;
import com.ymttest.database.sqlwapper.godmch.mch_relationWapper;
import com.ymttest.utils.EnvSetup;
/**
*批量获取代理推广关系
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_getAgentPromoteRelation{
	private static getAgentPromoteRelationBean bean;
	private static getAgentPromoteRelationCall call;
	Integer agent_promote_mchId=Integer.parseInt(EnvSetup.getData("agent_promote_mchId"));
	agent_typeWapper agentTypeWapper=new agent_typeWapper();
	mch_infoWapper infoWapper=new mch_infoWapper();
	country_infoWapper country_infoWapper=new country_infoWapper();
	mch_relationWapper relationWapper=new mch_relationWapper();
	invite_codeWapper codeWapper=new invite_codeWapper();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量获取代理推广关系");
	}

	@Before
	public void caseUp() {
		bean=new getAgentPromoteRelationBean();
		call=new getAgentPromoteRelationCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentPromoteRelation_001() {
		Logger.start(true,"批量获取代理推广关系-一级");
		try {
			
			Integer mchId=agent_promote_mchId;
			//申请 审核代理1
			applyAgentCall applyAgent =GodmchManageCallService.applyAndapproveAgent(mchId);
			editmchBean editmchBean=GodmchManageCallService.IniteditmachBean(applyAgent.getAgentId());
            GodmchManageCallService.editmachCall(editmchBean);
			
			getAgentPromotePicCall getAgentPromotePicCall=GodmchManageCallService.getAgentPromotePic(applyAgent.getAgentId());
			Thread.sleep(5000);
			registerByCodeBean registerBean=GodmchManageCallService.initRegisterByCode(mchId, getAgentPromotePicCall.getInviteCode());
			registerByCodeCall registerCall=GodmchManageCallService.registerByCodeCall(registerBean);
			Integer agentId2=registerCall.getRegMchId();
			Logger.comment("################ agentId2:"+agentId2);
		   
			bean.setAgentIdList(Lists.newArrayList(agentId2));
			bean.setMaxLevel(2);
			call.setData(bean);
			call.callService();	
			
			JSONArray jArray=call.getRelations();
			mch_relation relation=relationWapper.selectByPrimaryKey( bean.getAgentIdList().get(0), this.agent_promote_mchId).get(0);
			Logger.verifyEquals("200", call.getStatusCode1(), "验证statusCode");
			Map<String, Object> map = new HashMap();
			map.put("mchId", relation.getInviteCode());
			invite_code code = codeWapper.selectByCode(relation.getInviteCode());
			//invite_code表中的RoleType 关联 agent_type 表中的 AgentType 
			Logger.verifyEquals( code.getRoleType(), relation.getAgentType(), "验证AgentType");
			Logger.verifyEquals(1, relation.getAgentModel(), "验证AgentModel");
			Logger.verifyEquals(0, relation.getState(), "验证state");
			Logger.verifyEquals(true, relation.getSamePlatform(), "验证SamePlatform");
			Logger.verifyEquals(code.getMchId(), relation.getInviterId(), "验证inviterId");
			
			Logger.verifyEquals(new JSONObject(jArray.get(0).toString()).getInt("inviterId") , relation.getInviterId(), "验证inviterId");
			Logger.verifyEquals(new JSONObject(jArray.get(0).toString()).getInt("level") , 1, "验证level");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentPromoteRelation_002() {
		Logger.start(true,"批量获取代理推广关系-两级");
		try {
			
			Integer mchId=agent_promote_mchId;
			//申请 审核代理1
			applyAgentCall applyAgent =GodmchManageCallService.applyAndapproveAgent(mchId);
			editmchBean editmchBean=GodmchManageCallService.IniteditmachBean(applyAgent.getAgentId());
            GodmchManageCallService.editmachCall(editmchBean);
			//代理1 邀请代理2
			getAgentPromotePicCall getAgentPromotePicCall=GodmchManageCallService.getAgentPromotePic(applyAgent.getAgentId());
			Thread.sleep(5000);
			registerByCodeBean registerBean=GodmchManageCallService.initRegisterByCode(mchId, getAgentPromotePicCall.getInviteCode());
			registerByCodeCall registerCall=GodmchManageCallService.registerByCodeCall(registerBean);
			Integer agentId2=registerCall.getRegMchId();
			Logger.comment("################ agentId2:"+agentId2);
		   
			//代理1 邀请代理3
		    editmchBean=GodmchManageCallService.IniteditmachBean(agentId2);
            GodmchManageCallService.editmachCall(editmchBean);
			getAgentPromotePicCall=GodmchManageCallService.getAgentPromotePic(agentId2);
			Thread.sleep(5000);
			registerBean=GodmchManageCallService.initRegisterByCode(mchId, getAgentPromotePicCall.getInviteCode());
			registerCall=GodmchManageCallService.registerByCodeCall(registerBean);
			Integer agentId3=registerCall.getRegMchId();
			Logger.comment("################ agentId3:"+agentId3);
			
			bean.setAgentIdList(Lists.newArrayList(agentId3));
			bean.setMaxLevel(2);
			call.setData(bean);
			call.callService();	
			
			JSONArray jArray=call.getRelations();
			mch_relation relation=relationWapper.selectByPrimaryKey( bean.getAgentIdList().get(0), this.agent_promote_mchId).get(0);
			Logger.verifyEquals("200", call.getStatusCode1(), "验证statusCode");
			Map<String, Object> map = new HashMap();
			map.put("mchId", relation.getInviteCode());
			invite_code code = codeWapper.selectByCode(relation.getInviteCode());
			//invite_code表中的RoleType 关联 agent_type 表中的 AgentType 
			Logger.verifyEquals( code.getRoleType(), relation.getAgentType(), "验证AgentType");
			Logger.verifyEquals(1, relation.getAgentModel(), "验证AgentModel");
			Logger.verifyEquals(0, relation.getState(), "验证state");
			Logger.verifyEquals(true, relation.getSamePlatform(), "验证SamePlatform");
			Logger.verifyEquals(code.getMchId(), relation.getInviterId(), "验证inviterId");
			
			//分裂agentType 都是主商户的
			Logger.verifyEquals(new JSONObject(jArray.get(0).toString()).getInt("inviterId") , relation.getInviterId(), "验证inviterId");
			Logger.verifyEquals(new JSONObject(jArray.get(0).toString()).getInt("level") , 1, "验证level");
			
			//第二级
			relation=relationWapper.selectByPrimaryKey( agentId2, this.agent_promote_mchId).get(0);
			Logger.verifyEquals(new JSONObject(jArray.get(1).toString()).getInt("inviterId") , relation.getInviterId(), "验证inviterId");
			Logger.verifyEquals(new JSONObject(jArray.get(1).toString()).getInt("level") , 2, "验证level");
			
			Logger.verifyEquals(2,jArray.length() , "返回的长度");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentPromoteRelation_002_1() {
		Logger.start(true,"批量获取代理推广关系-两级-最多查询一级");
		try {
			Integer mchId=agent_promote_mchId;
			//申请 审核代理1
			applyAgentCall applyAgent =GodmchManageCallService.applyAndapproveAgent(mchId);
			editmchBean editmchBean=GodmchManageCallService.IniteditmachBean(applyAgent.getAgentId());
            GodmchManageCallService.editmachCall(editmchBean);
			//代理1 邀请代理2
			getAgentPromotePicCall getAgentPromotePicCall=GodmchManageCallService.getAgentPromotePic(applyAgent.getAgentId());
			Thread.sleep(5000);
			registerByCodeBean registerBean=GodmchManageCallService.initRegisterByCode(mchId, getAgentPromotePicCall.getInviteCode());
			registerByCodeCall registerCall=GodmchManageCallService.registerByCodeCall(registerBean);
			Integer agentId2=registerCall.getRegMchId();
			Logger.comment("################ agentId2:"+agentId2);
		   
			//代理1 邀请代理3
		    editmchBean=GodmchManageCallService.IniteditmachBean(agentId2);
            GodmchManageCallService.editmachCall(editmchBean);
			getAgentPromotePicCall=GodmchManageCallService.getAgentPromotePic(agentId2);
			Thread.sleep(5000);
			registerBean=GodmchManageCallService.initRegisterByCode(mchId, getAgentPromotePicCall.getInviteCode());
			registerCall=GodmchManageCallService.registerByCodeCall(registerBean);
			Integer agentId3=registerCall.getRegMchId();
			Logger.comment("################ agentId3:"+agentId3);
			
			bean.setAgentIdList(Lists.newArrayList(agentId3));
			bean.setMaxLevel(1);
			call.setData(bean);
			call.callService();			
			
			JSONArray jArray=call.getRelations();
			mch_relation relation=relationWapper.selectByPrimaryKey( bean.getAgentIdList().get(0), this.agent_promote_mchId).get(0);
			Logger.verifyEquals("200", call.getStatusCode1(), "验证statusCode");
			Map<String, Object> map = new HashMap();
			map.put("mchId", relation.getInviteCode());
			invite_code code = codeWapper.selectByCode(relation.getInviteCode());
			//invite_code表中的RoleType 关联 agent_type 表中的 AgentType 
			Logger.verifyEquals( code.getRoleType(), relation.getAgentType(), "验证AgentType");
			Logger.verifyEquals(1, relation.getAgentModel(), "验证AgentModel");
			Logger.verifyEquals(0, relation.getState(), "验证state");
			Logger.verifyEquals(true, relation.getSamePlatform(), "验证SamePlatform");
			Logger.verifyEquals(code.getMchId(), relation.getInviterId(), "验证inviterId");
			
			//分裂agentType 都是主商户的
			Logger.verifyEquals(new JSONObject(jArray.get(0).toString()).getInt("inviterId") , relation.getInviterId(), "验证inviterId");
			Logger.verifyEquals(new JSONObject(jArray.get(0).toString()).getInt("level") , 1, "验证level");
			
			Logger.verifyEquals(1,jArray.length() , "返回的长度");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentPromoteRelation_003() {
		Logger.start(true,"批量获取代理推广关系-agentIdList集合中含有主商户");
		try {
			Integer mchId=agent_promote_mchId;
			//申请 审核代理1
			applyAgentCall applyAgent =GodmchManageCallService.applyAndapproveAgent(mchId);
			editmchBean editmchBean=GodmchManageCallService.IniteditmachBean(applyAgent.getAgentId());
            GodmchManageCallService.editmachCall(editmchBean);
			
			getAgentPromotePicCall getAgentPromotePicCall=GodmchManageCallService.getAgentPromotePic(applyAgent.getAgentId());
			Thread.sleep(5000);
			registerByCodeBean registerBean=GodmchManageCallService.initRegisterByCode(mchId, getAgentPromotePicCall.getInviteCode());
			registerByCodeCall registerCall=GodmchManageCallService.registerByCodeCall(registerBean);
			Integer agentId2=registerCall.getRegMchId();
			Logger.comment("################ agentId2:"+agentId2);
		   
			bean.setAgentIdList(Lists.newArrayList(mchId));
			bean.setMaxLevel(2);
			call.setData(bean);
			call.callService();	
			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证statusCode");
			JSONArray jArray=call.getList();
			Logger.verifyEquals(0,jArray.length() , "返回的长度");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentPromoteRelation_004() {
		Logger.start(true,"批量获取代理推广关系-agentIdList集合为空");
		try {
			
			//bean.setAgentIdList(Lists.newArrayList(mchId));
			bean.setMaxLevel(2);
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("4001", call.getStatusCode1(), "验证statusCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentPromoteRelation_005() {
		Logger.start(true,"批量获取代理推广关系-maxLevel=0");
		try {
			Integer mchId=agent_promote_mchId;
			//申请 审核代理1
			applyAgentCall applyAgent =GodmchManageCallService.applyAndapproveAgent(mchId);
			editmchBean editmchBean=GodmchManageCallService.IniteditmachBean(applyAgent.getAgentId());
            GodmchManageCallService.editmachCall(editmchBean);
			
			getAgentPromotePicCall getAgentPromotePicCall=GodmchManageCallService.getAgentPromotePic(applyAgent.getAgentId());
			Thread.sleep(5000);
			registerByCodeBean registerBean=GodmchManageCallService.initRegisterByCode(mchId, getAgentPromotePicCall.getInviteCode());
			registerByCodeCall registerCall=GodmchManageCallService.registerByCodeCall(registerBean);
			Integer agentId2=registerCall.getRegMchId();
			Logger.comment("################ agentId2:"+agentId2);
		   
			bean.setAgentIdList(Lists.newArrayList(agentId2));
			bean.setMaxLevel(0);
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("4001", call.getStatusCode1(), "验证statusCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}